<?php

namespace App\Admin\Controllers;

use App\Models\Order;
use App\Models\Product;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;
use App\Admin\Extensions\Tools\OrderStatusGroup;
use Illuminate\Http\Request;

class OrderController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = 'Order';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Order());
        if(!empty($_GET['status'])){
            $grid->model()->where('status',intval($_GET['status']));
        }
        $grid->column('id', __('Id'));
        $grid->column('serial_number', __('Serial number'));
        // $grid->column('user_id', __('User id'));
        $grid->column('province', __('Province'));
        $grid->column('city', __('City'));
        $grid->column('district', __('District'));
        // $grid->column('street', __('Street'));
        $grid->column('home', __('Home'));
        $grid->column('phone', __('Phone'));
        $grid->column('name', __('Name'));
        $grid->column('status', __('Status'))->display(function ($status){
            $statusMap = [
                1 => '初始',
                4 => '网点接收',
                6 => '服务完成',
                8 => '服务取消',
            ];
            return isset($statusMap[$status])?$statusMap[$status]:'未知';
        });
        // $grid->column('remark', __('Remark'));
        // $grid->column('operate_user_id', __('Operate user id'));
        // $grid->column('operate_remark', __('Operate remark'));
        $grid->column('created_at', __('Created at'));
        // $grid->column('updated_at', __('Updated at'));
        $grid->column('product_id', __('Product'))->display(function ($productId){
            $product = Product::find($productId);
            return empty($product)?'未知':$product->name;
        });
        $grid->column('date', __('Serve date'));
        $grid->column('time', __('Serve time rang'));

        $grid->tools(function ($tools) {
            $tools->append(new OrderStatusGroup());
        });

        //查询
        $grid->filter(function ($filter) {
            // $filter->disableIdFilter();
            $filter->equal('serial_number','流水号');
            $filter->equal('phone','phone');
            $filter->equal('name','name');
        });

        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(Order::findOrFail($id));

        $show->field('id', __('Id'));
        $show->field('serial_number', __('Serial number'));
        $show->field('user_id', __('User id'));
        $show->field('province', __('Province'));
        $show->field('city', __('City'));
        $show->field('district', __('District'));
        $show->field('street', __('Street'));
        $show->field('home', __('Home'));
        $show->field('phone', __('Phone'));
        $show->field('name', __('Name'));
        $show->field('status', __('Status'));
        $show->field('remark', __('Remark'));
        $show->field('operate_user_id', __('Operate user id'));
        $show->field('operate_remark', __('Operate remark'));
        $show->field('created_at', __('Created at'));
        $show->field('updated_at', __('Updated at'));
        $show->field('coupon_code', __('Coupon code'));
        $show->field('serve_date', __('Serve date'));
        $show->field('serve_time_rang', __('Serve time rang'));

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new Order());

        $form->text('serial_number', __('Serial number'));
        $form->number('user_id', __('User id'));
        $form->text('province', __('Province'));
        $form->text('city', __('City'));
        $form->text('district', __('District'));
        $form->text('street', __('Street'));
        $form->text('home', __('Home'));
        $form->mobile('phone', __('Phone'));
        $form->text('name', __('Name'));
        $form->switch('status', __('Status'));
        $form->text('remark', __('Remark'));
        $form->number('operate_user_id', __('Operate user id'));
        $form->text('operate_remark', __('Operate remark'));
        $form->text('coupon_code', __('Coupon code'));
        $form->date('serve_date', __('Serve date'))->default(date('Y-m-d'));
        $form->text('serve_time_rang', __('Serve time rang'));

        return $form;
    }


    protected function init() 
    {

    }
}
